var app = angular.module('myApp', []);

app.controller('registerController', function($scope,$http) {
    $scope.register = function() {

    	console.log($scope.username);
    	console.log("正式姓名=>" + $scope.truename);
    	var formdata = {
    		"username" : $scope.username,
    		"truename" : $scope.truename,
    		"password" : $scope.password,
            "gender" : $scope.gender,
            "phone" : $scope.phone,
            "email" : $scope.email,
            "code" : $scope.code
    	};

    	console.log(formdata);

    	var url = $scope.getBasePath() + 'user/back_register';

    	$http({
    		method : 'post',
    		url : url,
    		data : formdata,
			headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
			transformRequest: function(obj) {
				var str = [];
				for(var p in obj){
					str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
				}
				console.log(str.join("*"));
				return str.join("&");
			}
    	}).
    	success(function(response){
    		console.log("success");
            console.log(response);
            alert(response.msg);
    		if (response.isSuccess) {
                location.href = $scope.getBasePath() + "login";
            }
    	}).
    	error(function(response){
    		console.log("error");
    		console.log(response);
    	});
    }

    $scope.sendMailCode = function(){
        var reg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
        console.log($scope.email);
        if (!reg.test($scope.email)) {
            alert("邮箱格式错误!");
            return ;
        }

        var url = $scope.getBasePath() + 'mail_check';

        $http({
            method : 'GET',
            url : url,
            params : {
                email : $scope.email
            },
        }).
        success(function(response) {
            console.log("success");
            alert(response.msg);
        }).
        error(function(response) {
            console.log("error");
            console.log(response);
        });
    }

    $scope.getBasePath = globalPath.getBasePath;
});